package net.sansi.v3correctserver.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.R;
import net.sansi.v3correctserver.dto.task.TaskDto;
import net.sansi.v3correctserver.task.V3Task;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Fang
 * create time : 2025/2/11
 * IDE : IntelliJ IDEA
 * <p>
 * 任务相关接口
 */
@Api(tags = "任务相关接口")
@RestController
@RequestMapping("/task")
@Validated
public class TaskController {

    /**
     * 停止任务
     *
     * @param req 请求参数
     * @return 是否停止成功
     */
    @ApiOperation("停止任务")
    @PostMapping("/interrupt")
    public R<String> interrupt(@RequestBody @Validated TaskDto.InterruptReq req) {
        V3Task<?, ?> task = AppHelper.getTaskManager().get(req.getTaskId());
        if (task == null || task.isFinish()) return R.success();
        if (req.getWait()) task.interruptWaitStop();
        else task.interrupt();
        return R.success();
    }
}
